package gov.child.service.impl;

import gov.child.dao.EnterpriseDeclareFormDao;
import gov.child.entities.po.EDepartment;
import gov.child.entities.po.EEnterpriseDeclareForm;
import gov.child.entities.po.EPersonalDeclareForm;
import gov.child.entities.vo.*;
import gov.child.service.EnterpriseDeclareFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author fzh
 * @since 2017/9/14
 */
@Service("enterpriseFormService")
public class EnterpriseDeclareFormServiceImpl implements EnterpriseDeclareFormService {

    @Autowired
    private EnterpriseDeclareFormDao enterpriseDeclareFormDao;

    @Override
    public EnterpriseDeclareForm findById(Integer id) {
        EEnterpriseDeclareForm form = enterpriseDeclareFormDao.findById(id);
        if (Objects.isNull(form)) return EnterpriseDeclareForm.EMPTY_ENTERPRISE_DECLARE_FORM;
        return EnterpriseDeclareForm.parseFrom(form);
    }

    @Override
    public EnterpriseDeclareForm findByUnicode(String code) {
        EEnterpriseDeclareForm form = enterpriseDeclareFormDao.findByUniformCode(code);
        if (Objects.isNull(form)) return EnterpriseDeclareForm.EMPTY_ENTERPRISE_DECLARE_FORM;
        return EnterpriseDeclareForm.parseFrom(form);
    }

    @Override
    public List findByUser(User user, Page page) {
        return BeanTransformer.parseFrom(EnterpriseDeclareForm.class, enterpriseDeclareFormDao.findByUserId(user.getUserId(), page));
    }

    @Override
    public List findByDepartment(Department department, Page page) {
        EDepartment dept = Department.parseTo(department);
        List l = enterpriseDeclareFormDao.findByDepartment(dept, page);
        return BeanTransformer.parseFrom(PersonalDeclareForm.class, l);
    }

    @Override
    public List findByDepartmentAndStatus(Department department, byte status, Page page) {
        EDepartment dept = Department.parseTo(department);
        List temp = enterpriseDeclareFormDao.findByDepartment(dept, page);
        List l = (List) temp.stream().filter(o -> ((EPersonalDeclareForm) o).getStatus() == status).collect(Collectors.toList());
        return BeanTransformer.parseFrom(PersonalDeclareForm.class, l);
    }

    @Override
    public boolean save(EnterpriseDeclareForm form) {
        EEnterpriseDeclareForm form1 = EnterpriseDeclareForm.parseTo(form);
        enterpriseDeclareFormDao.save(form1);
        return true;
    }

    @Override
    public boolean delete(EnterpriseDeclareForm form) {
        EEnterpriseDeclareForm form1 = EnterpriseDeclareForm.parseTo(form);
        enterpriseDeclareFormDao.delete(form1);
        return true;
    }

    @Override
    public boolean update(EnterpriseDeclareForm form) {
        EEnterpriseDeclareForm form1 = EnterpriseDeclareForm.parseTo(form);
        enterpriseDeclareFormDao.update(form1);
        return true;
    }

    @Override
    public Page getPage(int pageSize) {
        long total = enterpriseDeclareFormDao.total();
        return PageUtil.createPage(pageSize, total, 1);
    }
}
